package com.example.comment_oa.aspact;

import com.alibaba.fastjson.JSONObject;
import com.example.comment_oa.pojos.SysRole;
import com.example.comment_oa.pojos.SysUserRole;
import com.example.comment_oa.response.AjaxResponse;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Aspect
@Component
public class PermisionAspect {
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Before("@annotation(controllerDataScope)")
    public void doBefore(JoinPoint point, Permission controllerDataScope) throws Throwable
    {
        String permission = stringRedisTemplate.opsForValue().get("permission");
        System.out.println(permission);
        if(permission.indexOf(controllerDataScope.value())<0){
            throw new MyException("没有权限");
        }
    }

//    @AfterReturning(value = "@annotation(controllerDataScope)",returning = "ret")
//    public void doBefore(JoinPoint point, Permission controllerDataScope,AjaxResponse ret) throws Throwable
//    {
//        String permission = stringRedisTemplate.opsForValue().get("permission");
//        System.out.println(permission);
//        if(permission.indexOf(controllerDataScope.value())<0){
//            ret.setCode(401);
//        }
//    }

//    @Around(value = "@annotation(controllerDataScope)",returning = "ret")
//    public Object around(ProceedingJoinPoint pjp) {
//        String permission = stringRedisTemplate.opsForValue().get("permission");
//        if (permission.indexOf(controllerDataScope.value())<0) {//你的校验成功执行方法,失败方法就不用执行了
//            return pjp.proceed();
//        } else {
//            //可以返回你失败的信息也可以直接抛出校验失败的异常
//        }
//    }
}
